package com.example.l.zcq1;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_anniu;
    private EditText editText;
    private EditText editText2;
    CheckBox showPwd;
    CheckBox remember_account;
    protected void init(){
        btn_anniu = (Button) findViewById(R.id.login_button_log);
        editText = (EditText) findViewById(R.id.login_editText1_log);
        editText2 = (EditText) findViewById(R.id.login_editText2_log);
        showPwd=(CheckBox)findViewById(R.id.cbk_showpwd);
        remember_account=(CheckBox)findViewById(R.id.remember_account);
        SharedPreferences loginStatic=getSharedPreferences("loginStatic",MODE_PRIVATE);
        String uname=   loginStatic.getString("uname","");
        String upwd=   loginStatic.getString("upwd","");
        String reb=   loginStatic.getString("remember","0");
        editText.setText(uname);
        editText2.setText(upwd);
        if (reb.equals("1")){
            remember_account.setChecked(true);
        }else
            remember_account.setChecked(false);
    }
    protected void onEvent(){
        btn_anniu.setOnClickListener(this);
        showPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    editText2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                else
                    editText2.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        init();
        onEvent();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_button_log:
                String username = editText.getText().toString();
                String upwd=editText2.getText().toString();
                if (username.isEmpty()) {
                    Toast.makeText(this, "账号不能为空！", Toast.LENGTH_LONG).show();
                    return;
                }
                if (upwd.isEmpty()){
                    Toast.makeText(this, "密码不能为空！", Toast.LENGTH_LONG).show();
                    return;
                }
                SharedPreferences loginStatic=getSharedPreferences("loginStatic",MODE_PRIVATE);
                SharedPreferences.Editor edit=loginStatic.edit();
//                if (username.equals("admin")&&upwd.equals("123456")){
                    if(remember_account.isChecked()){
                        edit.putString("uname",username);
                        edit.putString("upwd",upwd);
                        edit.putString("remember","1");
                    }else
                    {
                        edit.clear();
                    }
                    edit.commit();
                    startActivity(new Intent(this,Main4kjActivity.class));
                    finish();
//                }else {
//                    Toast.makeText(this,"非法用户，请重新登录！",Toast.LENGTH_SHORT).show();
//                }
//                else {Intent intent=new Intent(Main2Activity.this,Main4kjActivity.class);
//                startActivity(intent);}
//
//                break;
        }
    }
}